package com.github.aysnc.sql.db.postgresql.encoders

import com.github.jasync.sql.db.postgresql.column.PostgreSQLColumnEncoderRegistry
import com.github.jasync.sql.db.postgresql.encoders.ExecutePreparedStatementEncoder
import com.github.jasync.sql.db.postgresql.messages.frontend.PreparedStatementExecuteMessage
import io.netty.util.CharsetUtil
import java.util.UUID
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class ExecutePreparedStatementEncoderSpec {

    val registry = PostgreSQLColumnEncoderRegistry()
    val encoder = ExecutePreparedStatementEncoder(CharsetUtil.UTF_8, registry)
    val sampleMessage = byteArrayOf(
            66,
            0,
            0,
            0,
            88,
            51,
            101,
            100,
            102,
            101,
            56,
            56,
            102,
            45,
            102,
            48,
            100,
            48,
            45,
            52,
            54,
            98,
            99,
            45,
            56,
            53,
            56,
            48,
            45,
            48,
            53,
            51,
            98,
            57,
            101,
            56,
            97,
            102,
            54,
            99,
            53,
            0,
            51,
            101,
            100,
            102,
            101,
            56,
            56,
            102,
            45,
            102,
            48,
            100,
            48,
            45,
            52,
            54,
            98,
            99,
            45,
            56,
            53,
            56,
            48,
            45,
            48,
            53,
            51,
            98,
            57,
            101,
            56,
            97,
            102,
            54,
            99,
            53,
            0,
            0,
            0,
            0,
            1,
            -1,
            -1,
            -1,
            -1,
            0,
            0,
            69,
            0,
            0,
            0,
            45,
            51,
            101,
            100,
            102,
            101,
            56,
            56,
            102,
            45,
            102,
            48,
            100,
            48,
            45,
            52,
            54,
            98,
            99,
            45,
            56,
            53,
            56,
            48,
            45,
            48,
            53,
            51,
            98,
            57,
            101,
            56,
            97,
            102,
            54,
            99,
            53,
            0,
            0,
            0,
            0,
            0,
            83,
            0,
            0,
            0,
            4,
            67,
            0,
            0,
            0,
            42,
            80,
            51,
            101,
            100,
            102,
            101,
            56,
            56,
            102,
            45,
            102,
            48,
            100,
            48,
            45,
            52,
            54,
            98,
            99,
            45,
            56,
            53,
            56,
            48,
            45,
            48,
            53,
            51,
            98,
            57,
            101,
            56,
            97,
            102,
            54,
            99,
            53,
            0
    )

    @Test
    fun `encoder should correctly handle the case where an encoder returns null`() {

        val uuid = UUID.fromString("3edfe88f-f0d0-46bc-8580-053b9e8af6c5")
        val message = PreparedStatementExecuteMessage(uuid, "select * from users", listOf(null), registry)

        val result = encoder.encode(message)

        val bytes = ByteArray(result.readableBytes()) { 0 }
        result.readBytes(bytes)

        assertThat(bytes).isEqualTo(sampleMessage)
    }
}
